package com.qf.business.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.business.system.mapper.EmployeeMapper;
import com.qf.business.system.service.EmpRoleTableService;
import com.qf.business.system.service.EmployeeService;
import com.qf.commons.redis.lock.aop.CsLock;
import com.qf.data.system.dto.EmployeeDto;
import com.qf.data.system.entity.EmpRoleTable;
import com.qf.data.system.entity.Employee;
import com.qf.data.system.vo.input.EmpSearchInput;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 职工表(Employee)表服务实现类
 *
 * @author makejava
 * @since 2022-05-26 16:16:37
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmpRoleTableService empRoleTableService;


    @Override
    @CsLock("'mylock_' + #entity.username")
    @Transactional
    public boolean save(Employee entity) {
        //密码加密
        String salt = BCrypt.gensalt();//生成随机盐值
        String pw = BCrypt.hashpw(entity.getPassword(), salt);
        entity.setPassword(pw);

        return super.save(entity);
    }

    @Override
    public Employee login(String username, String password) {
//        Employee employee = super.query()
//                .eq("username", username)
//                .one();
        Employee employee = employeeMapper.queryEmpAndRoleAndPower(username);

//        System.out.println("获取的职工信息：" + employee.getId() + " " + employee.getUsername() + " " + employee.getName());
//        System.out.println("获取职工的角色信息：");
//        List<Role> roles = employee.getRoles();
//        for (Role role : roles) {
//            System.out.println(role);
//        }
//
//        System.out.println("获取职工的权限信息：");
//        List<Power> powers = employee.getPowers();
//        for (Power power : powers) {
//            System.out.println(power);
//        }

        //密码的校验
        if(BCrypt.checkpw(password, employee.getPassword())) {
            return employee;
        }
        return null;
    }


    @Override
    public List<EmployeeDto> queryBySearch(EmpSearchInput searchInput) {
        return employeeMapper.queryBySearch(searchInput);
    }

    /**
     * 修改员工的角色
     * @param eId
     * @param rIds
     * @return
     */
    @Override
    @Transactional
    public int updateRoles(Long eId, List<Long> rIds) {
        //1、根据员工的id 删除员工角色中间表
        Map params = new HashMap<>();
        params.put("eid", eId);
        empRoleTableService.removeByMap(params);

        //2、新增员工 - 角色的中间记录（批量添加）
        for (Long rId : rIds) {
            EmpRoleTable empRoleTable = new EmpRoleTable()
                    .setEid(eId)
                    .setRid(rId);
            empRoleTableService.save(empRoleTable);
        }
        return 1;
    }
}


